//
//  YKComposeToolBar.m
//  youkan
//
//  Created by 吴高烽 on 16/4/1.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKComposeToolBar.h"

@interface YKComposeToolBar()
@property (nonatomic ,assign)BOOL flag;
@property (nonatomic ,weak)UIButton *swithButton;
@end
@implementation YKComposeToolBar
//!> `-(instancetype)init`和`-(void)awakeFromNib` 的方法都会调用`-(instancetype)initWithFrame:(CGRect)frame`


-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self setUpAllChilen];
    }
    return self;
}
-(void)setUpAllChilen{
//    相册 图片 提及 话题 表情 键盘
    [self setUpButtonItemWithImage:[UIImage imageNamed:@"compose_camerabutton_background"] highImage:[UIImage imageNamed:@"compose_camerabutton_background_highlighted"] type:YKComposeButtonTypeCamera];
    [self setUpButtonItemWithImage:[UIImage imageNamed:@"compose_toolbar_picture"] highImage:[UIImage imageNamed:@"compose_toolbar_picture_highlighted"] type:YKComposeButtonTypePicture];
    [self setUpButtonItemWithImage:[UIImage imageNamed:@"compose_mentionbutton_background"] highImage:[UIImage imageNamed:@"compose_mentionbutton_background_highlighted"] type:YKComposeButtonTypeMention ];
    [self setUpButtonItemWithImage:[UIImage imageNamed:@"compose_trendbutton_background"] highImage:[UIImage imageNamed:@"compose_trendbutton_background_highlighted"] type:YKComposeButtonTypeTrend   ];
    [self setUpButtonItemWithImage:[UIImage imageNamed:@"compose_emoticonbutton_background"] highImage:nil/*[UIImage imageNamed:@"compose_emoticonbutton_background_highlighted"]*/ type:YKComposeButtonTypeEmotion];
}
//!>  按钮的代理回调方法,也可以通过block来实现
-(void)clickButton:(UIButton*)btn{
    if ([_delegate respondsToSelector:@selector(composeToolBar:didClickBtn:)]) {
        [_delegate composeToolBar:self didClickBtn:btn.tag];
    }
}
-(void)swithEmotinoAndKeyboard{
    if (self.flag) {
        self.flag=NO;
        [self.swithButton setImage:[UIImage imageNamed:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];
//        [self.swithButton setImage:[UIImage imageNamed:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateHighlighted];
    }else{
        self.flag =YES;
        [self.swithButton setImage:[UIImage imageNamed:@"compose_keyboardbutton_background"] forState:UIControlStateNormal];
//        [self.swithButton setImage:[UIImage imageNamed:@"compose_keyboardbutton_background_highlighted"] forState:UIControlStateHighlighted];
    }
}
//!> 初始化时默认第一个为选中状态
-(void)setUpButtonItemWithImage:(UIImage *)image highImage:(UIImage *)highImage type:(YKComposeButtonType)type {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:highImage forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag =type;
    [self addSubview:btn];
    if (type ==YKComposeButtonTypeEmotion) {
        self.swithButton =btn;
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    NSInteger count =self.subviews.count;
    CGFloat x =0;
    CGFloat y =0;
    CGFloat w = self.width/count;
    CGFloat h = self.height;
    for (int i=0; i<count; i++) {
        UIButton *btn = self.subviews[i];
        x = i*w;
        btn.frame = CGRectMake(x, y, w, h);
    }
}
@end
